﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BusinessData.SystemSpecific;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using System.Diagnostics;
using System.IO.Packaging;

namespace TFSCustomConnector
{
    public partial class AttachNETService : IContextProperty
    {
        private static IMethodInstance methodInstance;
        private static ILobSystemInstance lobSystemInstance;
        private static IExecutionContext executionContext;

        #region IContextProperty implementation

        public IMethodInstance MethodInstance
        {
            get { return methodInstance; }
            set { methodInstance = value; }
        }

        public ILobSystemInstance LobSystemInstance
        {
            get { return lobSystemInstance; }
            set { lobSystemInstance = value; }
        }

        public IExecutionContext ExecutionContext
        {
            get { return executionContext; }
            set { executionContext = value; }
        }

        #endregion

        private static DynamicType GenerateDynamicItemStructure(IMethodInstance methodInstance)
        {
            string IdentifierName = null;
            DynamicType dt = new DynamicType();

            ITypeDescriptor typeDescriptor = (methodInstance.MethodInstanceType == MethodInstanceType.Finder) ?
                methodInstance.GetMethod().GetParameters()[1].GetRootTypeDescriptor().GetChildTypeDescriptors()[0] :
                methodInstance.GetMethod().GetParameters()[1].GetRootTypeDescriptor();

            foreach (ITypeDescriptor childDescpritor in typeDescriptor.GetChildTypeDescriptors())
            {
                dt.Add(childDescpritor.Name, null);
                if (childDescpritor.IsIdentifierSet) IdentifierName = childDescpritor.GetIdentifier().Name;
            }

            if (IdentifierName == null) throw (new ArgumentNullException("Identifier is absent"));

            return dt;
        }

        public static DynamicType[] ReadList(string filter)
        {
            string TfsConnURL = (string)lobSystemInstance.GetProperties()["TFSConnectionURL"];
            string TfsWIType = (string)methodInstance.GetMethod().GetDataClass().GetProperties()["TFSWorkItemType"]; //methodInstance.GetMethod().GetDataClass().Name
            string lobSystemName = lobSystemInstance.Name;

            if (!filter.Contains("SELECT"))
                filter = (string)methodInstance.GetMethod().GetDataClass().GetProperties()["DefaultWorkItemQuery"];

            //Log("ExecuteStatic", methodInstance.MethodInstanceType.ToString(), lobSystemName, TfsWIType);
            DynamicType template = GenerateDynamicItemStructure(methodInstance);
            DynamicType[] dynItemsList = AttachHelper.QueryAttachments(template, TfsConnURL, filter, lobSystemName, TfsWIType);
            return dynItemsList;
        }

        public static DynamicType ReadItem(string ID)
        {
            string TfsConnURL = (string)lobSystemInstance.GetProperties()["TFSConnectionURL"];
            string TfsWIType = (string)methodInstance.GetMethod().GetDataClass().GetProperties()["TFSWorkItemType"]; //methodInstance.GetMethod().GetDataClass().Name
            string lobSystemName = lobSystemInstance.Name;

            //Log("ExecuteStatic", methodInstance.MethodInstanceType.ToString(), lobSystemName, TfsWIType);
            DynamicType template = GenerateDynamicItemStructure(methodInstance);
            template = AttachHelper.FindAttachment(template, TfsConnURL, ID, lobSystemName, TfsWIType);
            return template;
        }

        public static System.IO.Stream AccessItem(string ID)
        {
            string TfsConnURL = (string)lobSystemInstance.GetProperties()["TFSConnectionURL"];
            string TfsWIType = (string)methodInstance.GetMethod().GetDataClass().GetProperties()["TFSWorkItemType"]; //methodInstance.GetMethod().GetDataClass().Name
            string lobSystemName = lobSystemInstance.Name;

            //Log("ExecuteStatic", methodInstance.MethodInstanceType.ToString(), lobSystemName, TfsWIType);

            System.IO.Stream stream = AttachHelper.DownloadAttachment(TfsConnURL, ID, lobSystemName, TfsWIType);
            return stream;
        }

        public static void Delete(string ID)
        {
            string TfsConnURL = (string)lobSystemInstance.GetProperties()["TFSConnectionURL"];
            string TfsWIType = (string)methodInstance.GetMethod().GetDataClass().GetProperties()["TFSWorkItemType"]; //methodInstance.GetMethod().GetDataClass().Name
            string lobSystemName = lobSystemInstance.Name;

            //Log("ExecuteStatic", methodInstance.MethodInstanceType.ToString(), lobSystemName, TfsWIType);

            AttachHelper.DeleteAttachment(TfsConnURL, ID, lobSystemName, TfsWIType);
        }
    }
}
